#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: henry_oulen@163.com
@file: main.py
@time: 2024/5/11 9:30
@desc: 
"""

import sys
import math
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPainter, QColor, QFont, QPen
from PyQt5.QtCore import Qt, QRectF, QDateTime

import sys
import math
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPainter, QColor, QFont, QPen
from PyQt5.QtCore import Qt, QRectF, QDateTime

class Clock(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 400, 400)
        self.setWindowTitle('简单时间')
        # self.move(1000,1000)
        # 添加标签显示中国标准时间
        # self.clock.move(50, 50)  # 将时钟窗口相对于程序窗口内部的位置设定为 (50, 50)
        self.cn_time_label = QLabel('中国标准时间', self)
        self.cn_time_label.setFont(QFont("Arial", 14))
        # self.cn_time_label.setGeometry(self.width()/2-58, self.height()+180, self.width()+400, 20)
        self.cn_time_label.setGeometry(int(self.width()/2-63), int(self.height()+180), int(self.width()+400), 20)

        self.time_label = QLabel(self)
        self.time_label.setFont(QFont("Arial", 14))
        # self.time_label.setGeometry(self.width()/2-50, self.height()+200, self.width()+400, 20)
        self.time_label.setGeometry(int(self.width()/2-50), int(self.height()+200), int(self.width()+400), 20)
        self.timerEvent(None)
        self.timer = self.startTimer(1000)
        self.resize(360, 700)  # 设置窗口初始大小为指定的长宽

        # 创建时钟窗口
        self.clock = QLabel(self)
        self.clock.setGeometry(50, 50, 300, 300)  # 设置时钟窗口在程序窗口内部的位置和大小
    def paintEvent(self, event):
        super().paintEvent(event)  # 调用父类的 paintEvent 方法
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        size = min(self.width(), self.height())
        radius = size / 2 - 15
        painter.translate(self.width() / 2, self.height() / 2)
        # Draw clock face
        painter.setPen(QPen(QColor(255, 255, 255)))
        painter.drawEllipse(round(-radius), round(-radius), round(2 * radius), round(2 * radius))
        # Draw hour ticks
        for i in range(12):
            hour_angle = (i * 30 - 90) * math.pi / 180  # Start from 12 o'clock
            hour_length = radius * 0.8
            x1 = round(hour_length * math.cos(hour_angle))
            y1 = round(hour_length * math.sin(hour_angle))
            x2 = round(radius * math.cos(hour_angle))
            y2 = round(radius * math.sin(hour_angle))
            painter.drawLine(x1, y1, x2, y2)
        # Draw clock face markings
        for i in range(0, 60):
            angle = (i - 15) * (360 / 60)
            angle_rad = angle * math.pi / 180
            if i % 5 == 0:
                painter.setPen(QPen(QColor(206, 210, 222)))  # Set color to (206, 210, 222) for hour markings
                painter.drawLine(round((radius - 10) * math.cos(angle_rad)), round((radius - 10) * math.sin(angle_rad)),
                                 round((radius - 20) * math.cos(angle_rad)), round((radius - 20) * math.sin(angle_rad)))
            else:
                painter.setPen(QPen(QColor(206, 210, 222)))  # Set color to (206, 210, 222) for minute markings
                painter.drawLine(round((radius - 10) * math.cos(angle_rad)), round((radius - 10) * math.sin(angle_rad)),
                                 round((radius - 15) * math.cos(angle_rad)), round((radius - 15) * math.sin(angle_rad)))
        font = QFont("Arial", 16, QFont.Bold)  # Increase font size to 16
        painter.setFont(font)
        painter.setPen(QColor(44, 45, 46))  # Set color to (44, 45, 46) for clock face numbers
        for i in range(1, 13):
            angle = math.radians(i * 30 - 90)
            x = (radius - 30) * math.cos(angle)
            y = (radius - 30) * math.sin(angle)
            text_rect = QRectF(x - 15, y - 8, 30, 16)  # Adjust text position and size
            painter.drawText(text_rect, Qt.AlignCenter, str(i))
        # Get current time
        current_time = QDateTime.currentDateTime()
        hour = current_time.time().hour()
        minute = current_time.time().minute()
        second = current_time.time().second()
        # Draw hour hand with thicker line
        hour_angle = ((hour % 12) * 30 + minute / 2 - 90) * math.pi / 180
        hour_length = radius * 0.5
        hour_x = round(hour_length * math.cos(hour_angle))
        hour_y = round(hour_length * math.sin(hour_angle))
        painter.setPen(QPen(QColor(43, 44, 46), 4))  # Increase pen width to 4 for hour hand
        painter.drawLine(0, 0, hour_x, hour_y)
        # Draw minute hand with thicker line
        minute_angle = (minute * 6 + second / 10 - 90) * math.pi / 180
        minute_length = radius * 0.7
        minute_x = round(minute_length * math.cos(minute_angle))
        minute_y = round(minute_length * math.sin(minute_angle))
        painter.setPen(QPen(QColor(78, 79, 83), 3))  # Increase pen width to 3 for minute hand
        painter.drawLine(0, 0, minute_x, minute_y)
        # Draw second hand
        second_angle = (second * 6 - 90) * math.pi / 180
        second_length = radius * 0.9
        second_x = round(second_length * math.cos(second_angle))
        second_y = round(second_length * math.sin(second_angle))
        painter.setPen(QPen(QColor(255, 0, 0)))
        painter.drawLine(second_x, second_y, 0, 0)  # Draw the second hand from the calculated end point to the center
        # Draw center circle
        painter.setBrush(QColor(255, 255, 255))
        painter.drawEllipse(-5, -5, 10, 10)

    def timerEvent(self, event):
        self.update()
        current_time = QDateTime.currentDateTime().toString("hh:mm:ss")
        self.time_label.setText(current_time)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = Clock()
    clock.show()
    sys.exit(app.exec_())
